"""
API主入口
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.routers import alarms_router
# 导入所有路由模块
from app.routers import auth_router, analytics_router
from app.routers import dashboard_router  # 数据大屏路由
from app.routers import device_events_router  # 添加设备事件和操作日志路由
from app.routers import devices_router  # 添加设备路由
from app.routers import enums_router
from app.routers import files_router
from app.routers import line_loss_router  # 线损管理路由
from app.routers import notifications_router
from app.routers import regions_router
from app.routers import report_router  # 报表管理路由
from app.routers import system_router
from app.routers import telemetry_router  # 遥测数据路由
from app.routers import work_orders_router  # 添加工单路由
from common.config import settings

app = FastAPI(
	title="Dingchen Smart Meter Data API",
	description="智能电表数据监控与管理平台 API",
	version="1.0.0",
	docs_url="/docs",
	redoc_url="/redoc"
)

# 添加CORS中间件
app.add_middleware(
	CORSMiddleware,
	allow_origins=settings.cors_origins.split(",") if settings.cors_origins else ["*"],
	allow_credentials=True,
	allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
	allow_headers=["*"],
)

# 包含基础路由 (以 /base 开头)
app.include_router(auth_router.router, prefix="/base")
app.include_router(regions_router.router, prefix="/base")
app.include_router(enums_router.router, prefix="/base")
app.include_router(files_router.router, prefix="/base")
app.include_router(notifications_router.router, prefix="/base")

# 包含业务路由 (以 /api/v1 开头)
app.include_router(system_router.router, prefix="/api/v1")
app.include_router(alarms_router.router, prefix="/api/v1")  # 注册告警路由
app.include_router(devices_router.router, prefix="/api/v1")  # 注册设备路由
app.include_router(telemetry_router.router, prefix="/api/v1")  # 注册遥测数据路由
app.include_router(work_orders_router.router, prefix="/api/v1")  # 注册工单路由
app.include_router(device_events_router.router, prefix="/api/v1")  # 注册设备事件和操作日志路由

app.include_router(analytics_router.router, prefix="/api/v1")  # 注册数据采集路由
app.include_router(line_loss_router.router, prefix="/api/v1")  # 注册线损管理路由

app.include_router(dashboard_router.router, prefix="/api/v1")  # 注册数据大屏路由
app.include_router(report_router.router, prefix="/api/v1")  # 注册报表管理路由


@app.get("/")
async def root():
	return {
		"message": "Dingchen Smart Meter Data API",
		"version": "1.0.0",
		"docs": "/docs",
		"redoc": "/redoc"
	}


if __name__ == "__main__":
	import uvicorn

	uvicorn.run(
		"api_main:app",
		host=settings.api_host,
		port=settings.api_port,
		reload=(settings.env == "dev"),
		log_level=settings.log_level.lower()
	)
